package com.zw.pdm.module.designtask.controller;

import com.zw.pdm.base.BaseController;
import com.zw.pdm.common.JsonResult;
import com.zw.pdm.entity.com.DsnProCommon;
import com.zw.pdm.entity.com.DsnTaskCommon;
import com.zw.pdm.entity.designtask.DsnControlIntent;
import com.zw.pdm.entity.designtask.DsnProStruct;
import com.zw.pdm.entity.production.MakeResult;
import com.zw.pdm.entity.rep.Result;
import com.zw.pdm.entity.sys.Role;
import com.zw.pdm.module.basic.service.DictCommonService;
import com.zw.pdm.module.designtask.mapper.DesignTaskMapper;
import com.zw.pdm.module.designtask.mapper.DsnProcessMapper;
import com.zw.pdm.module.designtask.model.DsnDrawChangeModel;
import com.zw.pdm.module.designtask.model.DsnProSingleModel;
import com.zw.pdm.module.designtask.model.DsnProcessModel;
import com.zw.pdm.module.designtask.service.DsnConIntService;
import com.zw.pdm.module.designtask.service.DsnProcessService;
import com.zw.pdm.module.report.mapper.SheetMapper;
import com.zw.pdm.module.report.model.Project;
import com.zw.pdm.module.system.mapper.DeptMapper;
import com.zw.pdm.util.ShiroUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

import static com.zw.pdm.constant.Constant.GROUP_SPECTYPE;

/**
 * @Description: 设计进度看板（多项目看板、单项目看板）
 * @Author: China.wsg
 * @Date: 2020/10/21 13:20
 */
@Controller
@RequestMapping("/designTask/process")
@SuppressWarnings("all")
public class DsnProcessController extends BaseController {

    @Autowired
    private DsnProcessService dsnProcessService;
    @Autowired
    private DeptMapper deptMapper;
    @Autowired
    private DsnConIntService dsnConIntService;
    @Autowired
    private DictCommonService dictCommonService;
    @Autowired
    private SheetMapper sheetMapper;
    @Autowired
    private DsnProcessMapper dsnProcessMapper;
    @Autowired
    private DesignTaskMapper designTaskMapper;

    @GetMapping("/processPage")
    @RequiresPermissions({"dsn:proBull:view"})
    public String processPage(Model model) {
        model.addAttribute("depts", deptMapper.findDsnDept());
        return "designtask/list/processPage";
    }

    /** 跳转任务看板*/
    @GetMapping("/tzrwList")
    @RequiresPermissions({"dsn:proBull:view"})
    public String tzrwList(Model model,String proCode) {
        Project project = sheetMapper.findProjectByCode(proCode).get(0);
        model.addAttribute("proId", project.getId());
        model.addAttribute("proCode", proCode);
        model.addAttribute("proName", project.getName());
        model.addAttribute("deptId", ShiroUtils.getUser().getDeptId());
        model.addAttribute("deptName", ShiroUtils.getUser().getBranchName());
        model.addAttribute("createName", ShiroUtils.getUser().getName());
        List<Role> roleList = dsnProcessMapper.findUserRoleByUserId(ShiroUtils.getUserId());
        String url = "";
        if ((roleList != null && roleList.size() > 0) || "admin".equals(ShiroUtils.getUser().getAccount())){
            url = "designtask/list/gantPage";
        }else {
            url = "designtask/add/createGantEditPage";
        }
        return url;
    }

    /**
     * 多项目看板列表
     */
    @ResponseBody
    @PostMapping("/processList")
    @RequiresPermissions({"dsn:proBull:view"})
    public JsonResult processList(DsnProcessModel dsnProcessModel) {
        List<Integer> ids = dsnProcessMapper.findProjectIdsByQuery(dsnProcessModel);
        // 没有符合要求的项目带入一个查询不到的条件，否则会查询全部记录
        if (ids.size() < 1) {
            ids.add(-1);
        }
        dsnProcessModel.setIds(StringUtils.join(ids.toArray(), ","));

        startPage();
        List<DsnProcessModel> list = dsnProcessService.findProcessListByQuery(dsnProcessModel);
        return jsonResult(list);
    }

    @GetMapping("/proSinglePage")
    @RequiresPermissions({"dsn:proBull:view"})
    public String proSinglePage(Integer proId, Model model) {
        Project project = designTaskMapper.findProjectById(proId);
        model.addAttribute("id", proId);
        model.addAttribute("code", project.getCode());
        model.addAttribute("name", project.getName());
        model.addAttribute("num", project.getNum());
        model.addAttribute("demander", project.getDemander());
        model.addAttribute("address", project.getAddress());
        DsnControlIntent dsnControlIntent = new DsnControlIntent();
        dsnControlIntent.setProCode(project.getCode());
        dsnControlIntent.setFileType(1);
        List<DsnControlIntent> dsnControlIntents = dsnConIntService.findControlIntentListByQuery(dsnControlIntent);
        model.addAttribute("filesCount", dsnControlIntents.size());
        dsnControlIntent.setFileType(3);
        List<DsnControlIntent> dsnDebugIntents = dsnConIntService.findControlIntentListByQuery(dsnControlIntent);
        model.addAttribute("filesDebugCount", dsnDebugIntents.size());
        List<DsnProCommon> dsnProCommons = dsnProcessService.findProPerformanceNote(proId);
        if (dsnProCommons.size() > 0) {
            model.addAttribute("performanceNote", dsnProCommons.get(0).getPerformanceNote());
        } else {
            model.addAttribute("performanceNote", "");
        }
        return "designtask/view/proSinglePage";
    }

    /**
     * 任务进度（单项目看板）
     *
     * @param dsnProSingleModel
     * @return
     */
    @ResponseBody
    @RequiresPermissions({"dsn:proBull:view"})
    @PostMapping("/intentFileList")
    public JsonResult findControlIntentByProId(String proCode,Integer fileType) {
        startPage();
        DsnControlIntent dsnControlIntent = new DsnControlIntent();
        dsnControlIntent.setProCode(proCode);
        dsnControlIntent.setFileType(fileType);
        List<DsnControlIntent> list = dsnConIntService.findControlIntentListByQuery(dsnControlIntent);
        return jsonResult(list);
    }

    /**
     * 根据条件查询部分
     *
     * @param proId
     * @return
     */
    @ResponseBody
    @RequiresPermissions({"dsn:proBull:view"})
    @GetMapping("/proStruct/{proId}")
    public List<DsnProStruct> findProStructByProId(@PathVariable("proId") Integer proId) {
        return dsnProcessService.findProStructByProId(proId);
    }

    /**
     * 任务进度（单项目看板）
     *
     * @param dsnProSingleModel
     * @return
     */
    @ResponseBody
    @RequiresPermissions({"dsn:proBull:view"})
    @PostMapping("/taskStatus")
    public JsonResult buyList(DsnProcessModel dsnProcessModel) {
        startPage();
        List<DsnProcessModel> list = dsnProcessService.findProProcessStatus(dsnProcessModel.getProId());
        return jsonResult(list);
    }

    /**
     * 外购件列表（单项目看板）
     *
     * @param dsnProSingleModel
     * @return
     */
    @ResponseBody
    @PostMapping("/buyList")
    @RequiresPermissions({"dsn:proBull:view"})
    public JsonResult buyList(DsnProSingleModel dsnProSingleModel) {
        startPage();
        dsnProSingleModel.setTaskType(1);   // 外购件
        List<DsnProSingleModel> list = dsnProcessService.findProSingleListByQuery(dsnProSingleModel);
        return jsonResult(list);
    }

    /**
     * 制作件列表（单项目看板）
     *
     * @param dsnProSingleModel
     * @return
     */
    @ResponseBody
    @PostMapping("/makeList")
    @RequiresPermissions({"dsn:proBull:view"})
    public JsonResult makeList(DsnProSingleModel dsnProSingleModel) {
        startPage();
        List<DsnProSingleModel> list = dsnProcessService.findMakeSingleListByQuery(dsnProSingleModel);
        return jsonResult(list);
    }

    /**
     * 图纸变更单（单项目看板）
     *
     * @param dsnProSingleModel
     * @return draw_change
     */
    @ResponseBody
    @PostMapping("/drawChangeList")
    @RequiresPermissions({"dsn:proBull:view"})
    public JsonResult drawChangeList(DsnProSingleModel dsnProSingleModel) {
        startPage();
        List<DsnProSingleModel> list = dsnProcessService.findDrawChangeSingleListByQuery(dsnProSingleModel);
        return jsonResult(list);
    }

    /**
     * 技术说明（单项目看板）
     *
     * @param dsnProSingleModel
     * @return draw_change
     */
    @ResponseBody
    @PostMapping("/tecNoteList")
    @RequiresPermissions({"dsn:proBull:view"})
    public JsonResult tecNoteList(DsnProSingleModel dsnProSingleModel) {
        startPage();
        List<DsnProSingleModel> list = dsnProcessService.findTecNoteListSingleListByQuery(dsnProSingleModel);
        return jsonResult(list);
    }

    /**
     * 任务状态编辑
     */
    @ResponseBody
    @PostMapping("/editTaskStatus")
    @RequiresPermissions(value = {"dsn:proNode:edit", "dsn:proControl:edit", "dsn:proDebug:edit", "dsn:proCost:edit"}, logical = Logical.OR)
    public JsonResult editTaskStatus(DsnTaskCommon dsnTaskCommon) {
        return jsonResult(dsnProcessService.editTaskStatusByQuery(dsnTaskCommon));
    }

    /**
     * 部分任务状态编辑
     */
    @ResponseBody
    @PostMapping("/editPartStatus")
    @RequiresPermissions({"dsn:proNode:edit"})
    public JsonResult editPartStatus(DsnTaskCommon dsnTaskCommon) {
        return jsonResult(dsnProcessService.editPartStatusByQuery(dsnTaskCommon));
    }

    /**
     * 绩效备注
     */
    @ResponseBody
    @PostMapping("/note")
    @RequiresPermissions({"dsn:proMents:edit"})
    public JsonResult performanceNoteEdit(DsnProCommon dsnProCommon) {
        return isNotNull(dsnProcessService.performanceNoteEdit(dsnProCommon));
    }

    @GetMapping("/outPage")
    @RequiresPermissions({"dsn:proBull:view"})
    public String outPage(String proCode,String proPart,String special, Model model) {
        model.addAttribute("specials", dictCommonService.findDictByGroupName(GROUP_SPECTYPE));
        Project project = sheetMapper.findProjectByCode(proCode).get(0);
        model.addAttribute("proCode", project.getCode());
        model.addAttribute("proName", project.getName());
        model.addAttribute("proNum", project.getNum());
        model.addAttribute("unit", project.getUnit());
        if (proPart.equals("机械")) {
            proPart = "";
            special = "机械";
        } else if (proPart.equals("流体")) {
            proPart = "";
            special = "流体";
        } else if (proPart.equals("自动化")) {
            proPart = "";
            special = "自动化";
        }
        model.addAttribute("proPart", proPart);
        model.addAttribute("special", special);
        return "designtask/view/outPage";
    }


    /**
     * 外购件输出表
     *
     * @param result
     * @return
     */
    @ResponseBody
    @PostMapping("/outResultList")
    @RequiresPermissions({"dsn:proBull:view"})
    public JsonResult outPage(Result result) {
        startPage();
        List<Result> list = dsnProcessService.findRepResultByQuery(result);
        return jsonResult(list);
    }

    @GetMapping("/makePage")
    @RequiresPermissions({"dsn:proBull:view"})
    public String makePage(String proCode,String proPart,String special, Model model) {
        model.addAttribute("specials", dictCommonService.findDictByGroupName(GROUP_SPECTYPE));
        Project project = sheetMapper.findProjectByCode(proCode).get(0);
        model.addAttribute("proCode", project.getCode());
        model.addAttribute("proName", project.getName());
        model.addAttribute("proNum", project.getNum());
        model.addAttribute("unit", project.getUnit());
        if (proPart.equals("机械")) {
            proPart = "";
            special = "机械";
        } else if (proPart.equals("流体")) {
            proPart = "";
            special = "流体";
        } else if (proPart.equals("自动化")) {
            proPart = "";
            special = "自动化";
        }
        model.addAttribute("proPart", proPart);
        model.addAttribute("special", special);
        return "designtask/view/makePage";
    }

    /**
     * 制作件输出表
     *
     * @param result
     * @return
     */
    @ResponseBody
    @PostMapping("/makeResultList")
    @RequiresPermissions({"dsn:proBull:view"})
    public JsonResult makePage(MakeResult result) {
        startPage();
        List<MakeResult> list = dsnProcessService.findMakeResultByQuery(result);
        return jsonResult(list);
    }

    @GetMapping("/drawPage")
    @RequiresPermissions({"dsn:proBull:view"})
    public String drawPage(String proCode,String proPart,String special, Model model) {
        model.addAttribute("specials", dictCommonService.findDictByGroupName(GROUP_SPECTYPE));
        Project project = sheetMapper.findProjectByCode(proCode).get(0);
        model.addAttribute("proCode", project.getCode());
        model.addAttribute("proName", project.getName());
        model.addAttribute("proNum", project.getNum());
        model.addAttribute("unit", project.getUnit());
        if (proPart.equals("机械")) {
            proPart = "";
            special = "机械";
        } else if (proPart.equals("流体")) {
            proPart = "";
            special = "流体";
        } else if (proPart.equals("自动化")) {
            proPart = "";
            special = "自动化";
        }
        model.addAttribute("proPart", proPart);
        model.addAttribute("special", special);
        return "designtask/view/drawPage";
    }

    /**
     * 图纸变更输出表
     *
     * @param result
     * @return
     */
    @ResponseBody
    @PostMapping("/drawResultList")
    @RequiresPermissions({"dsn:proBull:view"})
    public JsonResult drawPage(DsnDrawChangeModel result) {
        startPage();
        List<DsnDrawChangeModel> list = dsnProcessService.findDrawChangeDetailByQuery(result);
        return jsonResult(list);
    }

    /**
     * 项目说明列表
     *
     * @param dsnProSingleModel
     * @return
     */
    @ResponseBody
    @PostMapping("/explainList")
    @RequiresPermissions({"dsn:proexpla:view"})
    public JsonResult explainList(DsnControlIntent controlIntent) {
        startPage();
        List<DsnControlIntent> list = dsnProcessService.findExplainListByQuery(controlIntent);
        return jsonResult(list);
    }

    /**
     * 导入
     *
     * @param file 导入文件
     * @return JsonResult
     */
    @ResponseBody
    @PostMapping("/uploadFileExp")
    @RequiresPermissions({"dsn:proexpla:edit"})
    public JsonResult uploadFileExp(@RequestParam("proId") Integer proId, @RequestParam("branchId") Integer branchId,
                                    @RequestParam("file") MultipartFile file, @RequestParam("fileContent") String fileContent) {
        dsnProcessService.uploadFileExp(file, proId, fileContent, branchId);
        return success();
    }

    /**
     * 删除项目说明文件
     */
    @ResponseBody
    @PostMapping("/delExp")
    @RequiresPermissions(value = {"dsn:proexpla:del", "dsn:intent:delFile","dsn:proDebug:delFile"}, logical = Logical.OR)
    public JsonResult delExp(Integer id) {
        return jsonResult(dsnProcessService.delExplainById(id));
    }

    /**
     * 修改项目说明文件
     */
    @ResponseBody
    @PostMapping("/updExp")
    @RequiresPermissions(value = {"dsn:proexpla:edit", "dsn:intent:upload","dsn:proDebug:upload"}, logical = Logical.OR)
    public JsonResult updExp(DsnControlIntent controlIntent) {
        return jsonResult(dsnProcessService.updExplainById(controlIntent));
    }

    /**
     * 取得节点状态信息
     */
    @ResponseBody
    @PostMapping("/nodeStatus")
    @RequiresPermissions({"dsn:proBull:view"})
    public JsonResult nodeStatus(DsnTaskCommon dsnTaskCommon) {
        JsonResult jsonResult = new JsonResult();
        jsonResult.setType(JsonResult.ResultType.SUCCESS);
        jsonResult.setData(dsnProcessService.findNodeStatusByQuery(dsnTaskCommon));
        return jsonResult;
    }

    /**
     * 取得控制意图节点状态信息
     */
    @ResponseBody
    @PostMapping("/nodeConStatus")
    @RequiresPermissions({"dsn:proBull:view"})
    public JsonResult nodeConStatus(DsnTaskCommon dsnTaskCommon) {
        JsonResult jsonResult = new JsonResult();
        jsonResult.setType(JsonResult.ResultType.SUCCESS);
        jsonResult.setData(dsnProcessService.findNodeConStatusByQuery(dsnTaskCommon));
        return jsonResult;
    }

    /**
     * 项目部分列表
     *
     * @param dsnProSingleModel
     * @return
     */
    @ResponseBody
    @PostMapping("/partList")
    @RequiresPermissions({"dsn:proBull:view"})
    public JsonResult partList(Integer proId, Integer deptId) {
        startPage();
        List<DsnTaskCommon> list = dsnProcessService.findPartListByProId(proId, deptId);
        return jsonResult(list);
    }

}
